/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package imp.reciever.compressor;

import imp.reciever.fileMover.FileMover;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
 *
 * @author julian.pena
 */
public class ZipCompressor implements Compressor {

    private static ZipOutputStream zos;
    public FileMover fm;
    public String srcOrigen;
    public String srcDestino;
    
   public ZipCompressor(String origen, String destino) {
       this.srcOrigen = origen;       
       origen+=".zip";             
       this.srcDestino = destino;       
   }
    
    
    @Override
    public void action() throws IOException {
        compress();
    }

    @Override
    public void compress() throws IOException, FileNotFoundException{
        File file = new File(srcOrigen);
        System.out.println(srcOrigen);
        zos = new ZipOutputStream(new FileOutputStream(file + ".zip"));
        //Call recursion.
        recurseFiles(file);        
        //We are done adding entries to the zip archive,
        //so close the Zip output stream.
        zos.close();
        this.fm = new FileMover(srcOrigen+=".zip", srcDestino);
        fm.action();
    }
    
    /**
     * Recurses down a directory and its subdirectories to look for files to add
     * to the Zip. If the current file being looked at is not a directory, the
     * method adds it to the Zip file.
     */
    private static void recurseFiles(File file) throws IOException, FileNotFoundException {
        if (file.isDirectory()) {
            //Create an array with all of the files and subdirectories         
            //of the current directory.
            String[] fileNames = file.list();
            if (fileNames != null) {
                //Recursively add each array entry to make sure that we get
                //subdirectories as well as normal files in the directory.
                for (int i = 0; i < fileNames.length; i++) {
                    recurseFiles(new File(file, fileNames[i]));
                }
            }
        } //Otherwise, a file so add it as an entry to the Zip file.      
        else {
            byte[] buf = new byte[1024];
            int len;
            //Create a new Zip entry with the file's name.         


            ZipEntry zipEntry = new ZipEntry(file.toString());
            //Create a buffered input stream out of the file         


            //we're trying to add into the Zip archive.         

            FileInputStream fin = new FileInputStream(file);
            BufferedInputStream in = new BufferedInputStream(fin);
            zos.putNextEntry(zipEntry);
            //Read bytes from the file and write into the Zip archive.         


            while ((len = in.read(buf)) >= 0) {
                zos.write(buf, 0, len);
            }
            //Close the input stream.         


            in.close();
            //Close this entry in the Zip stream.         


            zos.closeEntry();
        }
    }

    @Override
    public InputStream getInputStream() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void setInputStream(InputStream inputStream) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public OutputStream getOutputStream() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void setOutputStream(OutputStream outputStream) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
